package com.lovo.config;

import com.lovo.util.MQUtil;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQConfig {
    //spring封装rabiitmq的代码 RabbitTemplate
//注入工厂（已经预加载）
    @Autowired
    CachingConnectionFactory connectionFactory;
    //rabbitmq模板
    @Bean(value = "rabbitTemplate")
    public RabbitTemplate rabbitTemplate(){
        return new RabbitTemplate(connectionFactory);
    }

    @Bean
    public Queue SupplyOrderQueue(){
        return new Queue(MQUtil.SupplyOrderQueue);
    }

    @Bean
    public DirectExchange directExchangeSupplyOrder(){
        return new DirectExchange(MQUtil.directExchangeSupplyOrder);
    }

    @Bean
    public Binding directExchangeSupplyOrderToOrder(Queue SupplyOrderQueue,DirectExchange directExchangeSupplyOrder){
        return BindingBuilder.bind(SupplyOrderQueue).to(directExchangeSupplyOrder).with(MQUtil.SupplyOrderKey);
    }

    //队列
    @Bean
    public Queue requestQueue() {
        return new Queue(MQUtil.REQUESTQUEUE);
    }

    //交换机
    @Bean
    public DirectExchange directExchangeOrder() {
        return new DirectExchange(MQUtil.DIRECTEXCHANGEREQUEST);
    }

    //绑定队列和交换机
    @Bean
    public Binding directExchangeOrderToOrderQueue(@Qualifier("requestQueue") Queue orderQueue, DirectExchange directExchangeOrder) {
        //队列绑定交换机设置key
        return BindingBuilder.bind(orderQueue).to(directExchangeOrder).with(MQUtil.REQUESTKEY);
    }


    //报价队列
    @Bean
    public Queue priceQueue() {
        return new Queue(MQUtil.priceQueue);
    }

    //交换机
    @Bean
    public DirectExchange priceExchangeOrder() {
        return new DirectExchange(MQUtil.priceExchangeOrder);
    }

    //报价绑定队列和交换机
    @Bean
    public Binding priceExchangeOrderPriceQueue(@Qualifier("priceQueue")Queue priceQueue, DirectExchange priceExchangeOrder) {
        //队列绑定交换机设置key
        return BindingBuilder.bind(priceQueue).to(priceExchangeOrder).with(MQUtil.priceKey);
    }

    @Bean
    public Queue SupplyOrderCscQueue(){
        return new Queue(MQUtil.SupplyOrderCscQueue);
    }

    @Bean
    public DirectExchange directExchangeSupplyOrderCsc(){
        return new DirectExchange(MQUtil.directExchangeSupplyOrderCsc);
    }

    @Bean
    public Binding directExchangeSupplyOrderCscToOrder(Queue SupplyOrderCscQueue,DirectExchange directExchangeSupplyOrderCsc){
        return BindingBuilder.bind(SupplyOrderCscQueue).to(directExchangeSupplyOrderCsc).with(MQUtil.SupplyOrderCscKey);
    }
}
